package com.zhinong.controller.admin;

import com.aliyuncs.ecs.model.v20140526.CreateDiskRequest;
import com.zhinong.dto.EmployeeDTO;
import com.zhinong.dto.EmployeePageQueryDTO;
import com.zhinong.dto.PageQueryDTO;
import com.zhinong.dto.ProductPageQueryDTO;
import com.zhinong.entity.Product;
import com.zhinong.result.PageResult;
import com.zhinong.result.Result;
import com.zhinong.service.ProductService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


@RestController
@Slf4j
@RequestMapping("/admin/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private RedisTemplate redisTemplate;


    @PostMapping
    public Result save(@RequestBody Product product){
        log.info("新增产品:{}",product);
        productService.save(product);
        return Result.success(product);
    }

    @GetMapping("/page")
    @ApiOperation("产品分页查询")
    //员工分页查询
    public Result<PageResult> page(ProductPageQueryDTO productPageQueryDTO){
        log.info("产品分页查询,参数为:{}",productPageQueryDTO);
//        String key =  "pageQuery_" + productPageQueryDTO.getPage() + "_" + productPageQueryDTO.getPageSize();
//        PageResult pageResult = (PageResult)redisTemplate.opsForValue().get(key);
//        if(pageResult != null){
//            return Result.success(pageResult);
//        }
        PageResult pageResult = productService.pageQuery(productPageQueryDTO);
//        redisTemplate.opsForValue().set(key,pageResult);

        return Result.success(pageResult);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("产品删除")
    public Result delete(@PathVariable Long id){
        log.info("删除产品:{}",id);
        productService.deleteById(id);
        return Result.success();
    }

    @PutMapping("/{id}")
    @ApiOperation("产品修改")
    public Result update(@RequestBody Product product){
        log.info("产品修改:{}",product);
        productService.update(product);
//        Set keys = redisTemplate.keys("pageQuery*");
//        redisTemplate.delete(keys);
        return Result.success(product);
    }

    @GetMapping("/ids")
    @ApiOperation("根据id查询产品")
    public Result<List<Product>> getByID(@RequestParam List<Long> ids){
        log.info("根据id查询产品:{}",ids);
        List<Product> product = productService.getByIds(ids);
        return Result.success(product);
    }
}
